#!/bin/sh
#
# /etc/init.d/scannedonlyd
# v1.0 18.08.2013
# Dmitry Troshenkov (troshenkov.d@gmail.com)
#
# description: Starts and stops the  scannedonlyd_clamav
# on the CentOS/RedHat/Fedora etc.
# http://olivier.sessink.nl/scannedonly/
#
# chkconfig: 345 92 8
#
# The location for exec file
exec=/usr/local/sbin/scannedonlyd_clamav
# Name
prog="scannedonlyd_clamav"

# Check for exec file exist
if [ ! -x $exec ] ; then
    echo -e " $exec NOT EXIST!\n"
exit 0
fi

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
    elif [ -f /etc/rc.d/init.d/functions ] ; then
        . /etc/rc.d/init.d/functions
    else
exit 0
fi

# Check for conf file exist
if [ -f /etc/$prog.conf ] ; then
  . /etc/$prog.conf
    elif [ -f /etc/$prog/$prog.conf ] ; then
        . /etc/$prog/$prog.conf
    else
  echo -e "$prog.conf in the /etc/ or /etc/$prog/ directory is not exist\n"
exit 0
fi

# Check for Socket and Quarantine folders exist
test -d ${socket%/*} || mkdir -p ${socket%/*}
test -d $quarantaine_dir || mkdir -p $quarantaine_dir

start () {
    if [ ! -f $pid_file ] ; then
            if [ -n "$socket" ] && [ -z "$port" ] ; then METHOD="-s $socket"
                elif [ -n "$port" ] && [ -z "$socket" ] ; then METHOD="-p $port"
                    else echo -e "Port or Socket is not set" ; exit 0
            fi
#
        daemon $exec $METHOD    -t $max_threads         \
                                -g $max_large_threads   \
                                -l $log_level           \
                                -b $treated_fale_size   \
                                -m $log_time            \
                                -r $scanroot_dir        \
                                -d $quarantaine_dir     \
                                -q $requests            \
                                -i $pid_file            \
                                -e $exclude_dir
#
        echo -e "Starting $prog $METHOD\n" ; exit 0
    else echo "$prog (pid `cat $pid_file`) is running already!"; exit 0
    fi

exit 0
}

stop () {
    killproc $exec
    echo -e "Stopping $prog"
    test -e $socket && rm -f $socket
    test -f $pid_file && rm -f $pid_file
}

status () {
    if [ -f $pid_file ] ; then
        echo "$prog (pid `cat $pid_file`) is running ..."
        else
            echo "$prog is not running"
    fi
exit 0
}

case $1 in
    start)
        start
        ;;
    stop)
        stop
        ;;
    restart)
        stop
        sleep 2
        start
        ;;
    status)
        status
        ;;
    *)
        echo "Usage: $0 {start|stop|status|restart}"
        exit 0
esac

exit 0

